#include "BPageSwitch.h"

BPageSwitch::BPageSwitch(QWidget* parent) :
    QWidget(parent)
{
    this->resize(200, 200);
    this->setMinimumHeight(200);
    this->setMinimumWidth(200);
    //设置sizePolicy
    QSizePolicy sizePolicy = this->sizePolicy();
    sizePolicy.setHorizontalPolicy(QSizePolicy::Preferred);
    sizePolicy.setVerticalPolicy(QSizePolicy::Preferred);
    this->setSizePolicy(sizePolicy);
    //设置layout
    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->setContentsMargins(6, 6, 6, 6);
    this->setLayout(layout);
    //设置样式表
    this->setStyleSheet("background-color: rgb(170, 183, 184 );border-radius:12px;");
    //设置背景
    m_bgWight = new QWidget(this);
    m_bgWight->resize(this->size());
    m_bgWight->setStyleSheet("border-radius:0px;background-color:#00000000");
    m_bgWight->lower();
    m_bgWight->show();
    //允许鼠标跟踪
    this->setMouseTracking(true);
}

BPageSwitch::~BPageSwitch()
{

}

void BPageSwitch::setCenter()
{
    if (false == m_isCenter) {
        m_isCenter = true;
    }
}

void BPageSwitch::resetCenter()
{
    if (true == m_isCenter) {
        m_isCenter = false;
    }
}

void BPageSwitch::enterEvent(QEvent *event)
{
    Q_UNUSED(event);
    if (!m_isCenter) {
        m_bgWight->setStyleSheet("border-radius:12px;background-color:#1A7FFFD4");
        m_bgWight->raise();
    }
}

void BPageSwitch::leaveEvent(QEvent *event)
{
    Q_UNUSED(event);
    m_bgWight->setStyleSheet("border-radius:12px;background-color:#00000000");
    m_bgWight->lower();
}

void BPageSwitch::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton && m_isPressed == false) {
        m_isPressed = true;
        qDebug() << "click";
    }
    if (true == m_isPressed && false == m_isCenter) {
        qDebug() << "I want to center";
        emit requestCenter(m_id);
    }
}

void BPageSwitch::mouseReleaseEvent(QMouseEvent *event)
{
    m_isPressed = false;
}

void BPageSwitch::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);
    m_bgWight->resize(this->size());
}
